<template>
    <div class="row list-panel">
        <order-detail :showOrderDetail="showOrderDetail" :order="order" :products="products" :orderId="orderId"
            @onCancelNewModal="onCancel()"></order-detail>
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <ol class="breadcrumb">
                        <li><a>财务管理</a></li>
                        <li><a>财务统计</a></li>
                        <li><a>订单统计</a></li>
                    </ol>
                </div>
                <div class="content-wrapper">
                    <el-form :inline="true" :model="formInline" class="demo-form-inline" label-width="80px" size="small">
                        <el-form-item label="团名称">
                            <el-input v-model="formInline.storeName" placeholder="团名称"></el-input>
                        </el-form-item>
                        <el-form-item label="下单客户">
                            <el-input v-model="formInline.realName" placeholder="下单客户"></el-input>
                        </el-form-item>
                        <el-form-item label="付款日期">
                            <el-col :span="11">
                                <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="formInline.fromDate"
                                    style="width: 100%;"></el-date-picker>
                            </el-col>
                            <el-col class="line" :span="2" style="text-align: center;font-size: 12px">到</el-col>
                            <el-col :span="11">
                                <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="formInline.toDate"
                                    style="width: 100%;"></el-date-picker>
                            </el-col>
                        </el-form-item>

                        <el-form-item style="float: right;">
                            <a class="revision-btn" href="javaScript:void(0)" @click="onSubmit()">搜 索</a>
                        </el-form-item>
                    </el-form>
                    <div class="page-panel helping--panel">
                        <h3 class="page-panel-title"> 柱状统计
                        </h3>
                        <div class="page-panel-cont">
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="ibox float-e-margins" style="padding:0px;padding-top:20px;">
                                        <div class="ibox-title">
                                            <h5>订单</h5>
                                            <div class="pull-right">
                                                <div class="btn-group">
                                                    <button type="button" class="btn btn-xs btn-white active">天</button>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="ibox-content">
                                            <div class="row">
                                                <div class="col-sm-9">
                                                    <div class="flot-chart">
                                                        <!-- <div class="flot-chart-content" id="flot-dashboard-chart"></div> -->
                                                        <bar-chart :chart-data="chart_info"></bar-chart>
                                                    </div>
                                                </div>
                                                <div class="col-sm-3">
                                                    <ul class="stat-list">
                                                        <li>
                                                            <h2 class="no-margins">{{analysis.count}} 单</h2>
                                                            <small>订单数量</small>
                                                            <div class="progress progress-mini">
                                                                <div style="width: 48%;" class="progress-bar"></div>
                                                            </div>
                                                        </li>
                                                        <li>
                                                            <h2 class="no-margins ">{{analysis.completeAmount}} 元</h2>
                                                            <small>近一个月订单金额</small>
                                                            <div class="progress progress-mini">
                                                                <div style="width: 60%;" class="progress-bar"></div>
                                                            </div>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <h3 class="page-panel-title"> 列表统计
                    </h3>
                    <template v-if="data != null">
                        <el-table ref="multipleTable" :data="data.list" tooltip-effect="dark" :border="true" class="list-table"
                            :header-cell-style="{'background-color': '#F5F5F6'}" max-height="600" style="width: 100%">
                            <template v-for="columnName in columnNames">
                                <template v-if="columnName.prop === 'orderStatus'">
                                    <el-table-column align="center" :label="columnName.label" :prop="columnName.prop">
                                        <template slot-scope="scope">
                                            <span style="color:#1ab394;" v-if="scope.row.orderStatus === 0">已取消</span>
                                            <span style="color:#F00;" v-if="scope.row.orderStatus === 1">待付款</span>
                                            <span style="color:#F00;" v-if="scope.row.orderStatus === 2">待发货</span>
                                            <span style="color:#F00;" v-if="scope.row.orderStatus === 3">待自提</span>
                                            <span style="color:#1ab394;" v-if="scope.row.orderStatus === 4">已完成</span>
                                            <span style="color:#1ab394;" v-if="scope.row.orderStatus === 5">已退款</span>
                                        </template>
                                    </el-table-column>
                                </template>
                                <template v-else>
                                    <el-table-column :key="columnName.prop" :label="columnName.label" align="center"
                                        :prop="columnName.prop">
                                    </el-table-column>
                                </template>
                            </template>
                            <el-table-column label="操作" align="center">
                                <template slot-scope="scope">
                                    <el-button type="text" size="small" @click="orderDetail(scope.row)">详情</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                    <el-pagination style="float: right;margin-top: 10px" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange" :current-page.sync="data.currPage" :page-size="data.pageSize"
                        layout="total, prev, pager, next" :total="data.totalCount">
                    </el-pagination>

                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import BarChart from "@/components/echart/orderchart.vue";
    import orderDetail from '@/views/order/orderDetail'
    export default {
        name: 'OrderAnalysis',
        components: {
            BarChart,
            orderDetail
        },
        data() {
            return {
                formInline: {
                    fromDate: null,
                    toDate: null,
                    storeName: null,
                    realName: null
                },
                columnNames: [{
                    label: '订单号',
                    prop: 'orderNumber',
                    width: 180
                }, {
                    label: '下单客户',
                    prop: 'user.realName',
                    width: 120
                }, {
                    label: '联系电话',
                    prop: 'user.mobile',
                    width: 120
                }, {
                    label: '订单金额(元)',
                    prop: 'totalAmount',
                    width: 100
                }, {
                    label: '订单状态',
                    prop: 'orderStatus',
                    width: 100
                }, {
                    label: '所属团',
                    prop: 'store.storeName',
                    width: 180
                }, {
                    label: '创建日期',
                    prop: 'createTime',
                    width: 120
                }, {
                    label: '付款日期',
                    prop: 'payTime',
                    width: 120
                }, {
                    label: '发货日期',
                    prop: 'shipmentTime',
                    width: 120
                }],
                multipleSelection: [],
                data: {},
                query: {
                    page: ''
                },
                chart_info: {
                    date: [],
                    value: []
                },
                analysis: {},
                order: {
                    order: {},
                    orderGoodsList: [],
                    user: {},
                    store: {}
                },
                orderId: null,
                products: {
                    list: []
                },
                showOrderDetail: false
            }
        },
        mounted: function() {
            this.listAllRefund();
            this.getOrderInfo();
        },
        methods: {
            async onSubmit() {
                let res = await this.$api.get('/analysis/order/list', this.formInline);
                if (res.code === 0) {
                    this.data = res.page;
                } else {
                    this.$message.error(res.msg);
                }
            },
            onCancel() {
                this.showOrderDetail = false
            },
            handleSizeChange(val) {},
            handleCurrentChange(val) {
                this.query.page = val;
                this.listAllRefund();
            },
            async orderDetail(item) {
                let res1 = await this.$api.get('/mall/ordergoods/list', {
                    orderId: item.orderId
                });
                let res2 = await this.$api.get('/mall/order/info/' + item.orderId)
                this.order = res2.order
                this.orderId = item.orderId.toString()
                this.products = res1.page
                this.showOrderDetail = true
            },
            async listAllRefund() {
                let res = await this.$api.get('/analysis/order/list', this.query);
                if (res.code === 0) {
                    this.data = res.page;
                } else {
                    this.$message.error(res.msg);
                }
            },
            async getOrderInfo() {
                this.chart_info = {
                    date: [],
                    value: []
                }
                let res = await this.$api.get('/analysis/order')
                this.analysis = res.result
                for (let i = 0; i < res.result.trendList.length; i++) {
                    this.chart_info.date.push(res.result.trendList[i].payTime)
                    this.chart_info.value.push(res.result.trendList[i].completeAmount)
                }
            }
        }
    }
</script>

<style scoped>
    .list-panel .content-wrapper /deep/ .el-form-item__label {
        font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        color: #666;
        font-size: 12px;
        font-weight: normal;
    }

    .list-table {
        font-size: 12px;
    }
</style>
